#!/bin/bash

# Kubernetes离线包下载脚本

# 检查操作系统类型
detect_os() {
    if [ -f /etc/ky10-release ]; then
        echo "ky10"
    elif [ -f /etc/lsb-release ]; then
        echo "ubuntu"
    else
        echo "unknown"
    fi
}

# 检查是否为root用户
if [ "$(id -u)" != "0" ]; then
   echo "请使用root用户运行此脚本"
   exit 1
fi

# 创建离线包目录
mkdir -p /opt/k8s-offline

# 下载Docker离线包
download_docker() {
    echo "正在下载Docker离线包..."
    os_type=$(detect_os)
    
    if [ "$os_type" = "ky10" ]; then
        yum install -y yum-utils
        yumdownloader --destdir=/opt/k8s-offline docker-ce docker-ce-cli containerd.io
    else
        apt-get update
        apt-get install -y dpkg-dev
        apt-get download -o Dir::Cache::archives=/opt/k8s-offline docker-ce
    fi
    
    echo "Docker离线包下载完成"
}

# 下载Kubernetes离线包
download_k8s() {
    echo "正在下载Kubernetes离线包..."
    os_type=$(detect_os)
    
    if [ "$os_type" = "ky10" ]; then
        yumdownloader --destdir=/opt/k8s-offline kubelet kubeadm kubectl
    else
        apt-get download -o Dir::Cache::archives=/opt/k8s-offline kubelet kubeadm kubectl
    fi
    
    echo "Kubernetes离线包下载完成"
}

# 下载网络插件离线包
download_network() {
    echo "正在下载网络插件离线包..."
    curl -L https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml -o /opt/k8s-offline/kube-flannel.yml
    echo "网络插件离线包下载完成"
}

# 主执行流程
main() {
    download_docker
    download_k8s
    download_network
    
    echo "所有离线包已下载到/opt/k8s-offline目录"
    echo "请将此目录复制到目标机器进行离线安装"
}

# 执行主函数
main